/**
 * (#)com.ruoyi.common.utils.poi.ExcelCustomExportUtil.java
 * Create time 2024-12-04 21:14
 * Create by
 * Copyright © zhuxiaoxiang 版权所有 All Rights Reserved.
 */

package com.ruoyi.common.utils.poi;/**
 *
 * @author zhuxiaoxiang
 * @date 2024/12/4 21:14
 */

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.poi.ss.usermodel.CellStyle.*;
/**
 *
 * @author
 * @since JDK1.8
 */
public class ExcelCustomExportUtil {


    public static void exportExcel(HttpServletResponse response, List<String> headers, List<List<Object>> data){
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("sheet1");
        Map<String, CellStyle> styleMap = createStyles(workbook);
        CellStyle style = sheet.getWorkbook().createCellStyle();

        Row headerRow = sheet.createRow(0);
        for(int i=0;i<headers.size();i++){
            Cell cell = headerRow.createCell(i);
            cell.setCellStyle(styleMap.get("header"));
            cell.setCellValue(headers.get(i));
        }
        int rowIndex = 1;
        for(List<Object> rowData:data){
            Row row = sheet.createRow(rowIndex++);

            for (int i=0;i<rowData.size();i++){
                Cell cell = row.createCell(i);
                cell.setCellValue(rowData.get(i).toString());
            }

        }

//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Co");
//        try(OutputStream outputStream = response.getOutputStream()) {
//            workbook.write(outputStream);
//
//        }
    }

    private static Map<String, CellStyle> createStyles(Workbook wb) {
        Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
        CellStyle style=wb.createCellStyle();
//        style.setAlignment(CellStyle);
//        style.setVerticalAlignment(VERTICAL CENTER);
//        FonttitleFont=wb.createFont();
//        titleFont.setFontName("Arial");
//        titleFont.setFontHeightInPoints((short)16);
//        titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
//        style.setFont(titleFont);
//        styles.put("title",style);
//        style=wb.createCellStyle();
//        style.setVerticalAlignment(VERTICAL_CENTER);
//        style.setBorderRight(CellStyle.BORDER_THIN);
//        style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
//        style.setBorderLeft(CellStyle.BORDER_THIN);
//        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex();
//        style.setBorderTop(CellStyle.BORDER_THIN);
//        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
//        style.setBorderBottom(CellStyle.BORDER_THIN);
//        style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
//        Font dataFont =wb.createFont();
//        dataFont.setFontName("Arial");
//        dataFont.setFontHeightInPoints(short)10);
//        style.setFont(dataFont);
//        styles.put("data",style);
//        style=wb.createCellStyle();
//        style.cloneStyleFrom(styles.get("data");
//        style.setAlignment(CellStyle.ALIGN_LEFT);
//        styles.put("datal",style);
//        style=wb.createCellStyle();
//        style.cloneStyleFrom(styles.get("data"));
//        style.setAlignment(ALIGN_CENTER);
//        styles.put("data2",style);
//        style=wb.createCellStyle();
//        style.cloneStyleFrom(styles.get("data"));
//        style.setAlignment(CellStyle.ALIGN_RIGHT);
        return styles;
    }

    }
